package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProductionLineDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.entity.Item;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.ItemService;
import com.jyu.apts.service.OrganizationService;
import com.jyu.apts.service.UserService;

@RestController
public class ItemProductionLineController {

	@Autowired
	ItemProductionLineService itemProductionLineService;
	@Autowired
	ItemService itemService;
	@Autowired
	OrganizationService organizationService;
	@Autowired 
	UserService userService;
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/itemProductionLine_html")
	public ModelAndView itemProductionLine_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/itemProductionLine.html");
		return mv;
	}
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/addItemProductionLine")
	public ResultDto addItemProductionLine(String name,long itemId) {
		Item item = itemService.getOne(itemId);
		if(item == null) {
			return new ResultDto("商品不存在",null,2);
		}
		ItemProductionLine itemProductionLine = new ItemProductionLine();
		itemProductionLine.setName(name);
		itemProductionLine.setItem(item);
		itemProductionLineService.save(itemProductionLine);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/upItemProductionLine")
	public ResultDto upItemProductionLine(long id,String name,long itemId) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(id);
		if(itemProductionLine == null) {
			return new ResultDto("商品生产线不存在!",null,2);
		}
		Item item = itemService.getOne(itemId);
		if(item == null) {
			return new ResultDto("商品不存在",null,2);
		}
		itemProductionLine.setItem(item);
		itemProductionLine.setName(name);
		itemProductionLineService.save(itemProductionLine);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/delItemProductionLine")
	public ResultDto delItemProductionLine(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<ItemProductionLine> itemProductionLines = itemProductionLineService.findAllById(l_ids);
		itemProductionLineService.delete(itemProductionLines);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/getItemProductionLinePage")
	public TableDto<ItemProductionLineDto> getItemProductionLinePage(Integer pageNumber,Integer pageSize,Long organizationId,Long itemId,String search) {
		Page<ItemProductionLine> pageResult = itemProductionLineService.getItemProductionLinePage(pageNumber, pageSize, organizationId, itemId, search);
		List<ItemProductionLine> itemProductionLines = pageResult.getContent();
		List<ItemProductionLineDto> itemProductionLineDtos = new ArrayList<ItemProductionLineDto>();
		itemProductionLines.forEach(itemProductionLine->itemProductionLineDtos.add(new ItemProductionLineDto().convert(itemProductionLine)));
		return new TableDto<ItemProductionLineDto>(itemProductionLineDtos,pageResult.getTotalElements());
	}
	
	@RequiresPermissions("itemProductionLine_html")
	@RequestMapping("/getAllItemProductionLineByItem")
	public List<ItemProductionLineDto> getAllItemProductionLineByItem(long itemId) {
		Item item = itemService.getOne(itemId);
		List<ItemProductionLineDto> itemProductionLineDtos = new ArrayList<ItemProductionLineDto>();
		List<ItemProductionLine> itemProductionLines = item.getItemProductionLines();
		itemProductionLines.forEach(itemProductionLine->itemProductionLineDtos.add(new ItemProductionLineDto().convert(itemProductionLine)));
		return itemProductionLineDtos;
	}
	
}
